package com.hymjweb.demo.hymj.finance_manage.pay.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.framework.util.DoubleUtil;
import com.hymjweb.demo.hymj.finance_manage.pay.bean.CwBillPayClearedPojo;
import com.hymjweb.demo.hymj.finance_manage.pay.mapper.CwPayMapper;
import com.hymjweb.demo.hymj.master_bill_manage.auto.service.BMasterBillAutoService;
import com.hymjweb.demo.hymj.master_bill_manage.flight.service.BMasterBillFlightService;
import com.hymjweb.demo.hymj.native_bill_manage.service.BNativeBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CwPayService extends BaseService {

    @Autowired
    private CwPayMapper cwPayMapper;

    @Autowired
    private BNativeBillService bNativeBillService;

    @Autowired
    private BMasterBillFlightService bMasterBillFlightService;

    @Autowired
    private BMasterBillAutoService bMasterBillAutoService;


    public Object getPageList(Map param) {
        // 设置公共查询参数,加上部门
        this.setQueryCommParam(param);
        this.pageListInit(param);
        List list = cwPayMapper.findAll(param);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }


    public Object getNativeBillPageList(Map param) {
        return bNativeBillService.getPageList(param);
    }

    public Object getAirMasterBillPageList(Map param) {
        return bMasterBillFlightService.getPageList(param);
    }

    public Object getAutoMasterBillPageList(Map param) {
        return bMasterBillAutoService.getPageList(param);
    }


    @Transactional(rollbackFor = {Exception.class})
    public int clearedBillCharge(JSONObject clearedObj) throws Exception {
        //{"clearChargeTypeId":382,"idList":[{"billId":"100120","billAmount":100},{"billId":"100118","billAmount":20},{"billId":"100117","billAmount":30}],"billTypeId":371}

        Long clearChargeTypeId = DataTypeUtil.toLongObject(clearedObj.get("clearChargeTypeId"));
        Long billTypeId = DataTypeUtil.toLongObject(clearedObj.get("billTypeId"));
        Long statusId = DataTypeUtil.toLongObject(clearedObj.get("statusId"));
        JSONArray idArray = clearedObj.getJSONArray("idList");
        List<Map> idList = idArray.toJavaList(Map.class);


        Date currentTime = DateUtil.getSysDateTime();
        String czyId = this.getLoginCzyId();

        List<CwBillPayClearedPojo> cwBillPayClearedPojos = new ArrayList<>();

        String billIds ="";
        for(Map map:idList){
            String billId = DataTypeUtil.toStringObject(map.get("billId"));
            Double billAmount = DataTypeUtil.toDouble(map.get("billAmount"));

            CwBillPayClearedPojo cwBillPayClearedPojo = new CwBillPayClearedPojo();
            cwBillPayClearedPojo.setId(this.getSysStrId());
            cwBillPayClearedPojo.setBillId(billId);
            cwBillPayClearedPojo.setBillTypeId(billTypeId);
            cwBillPayClearedPojo.setBillPayClearedTypeId(clearChargeTypeId);
            cwBillPayClearedPojo.setBillPayClearedAmount(DoubleUtil.round2(billAmount));
            cwBillPayClearedPojo.setCreateTime(currentTime);
            cwBillPayClearedPojo.setCreateUserId(czyId);
            cwBillPayClearedPojo.setUpdateTime(currentTime);
            cwBillPayClearedPojo.setUpdateUserId(czyId);

            cwBillPayClearedPojos.add(cwBillPayClearedPojo);

            billIds += DataTypeUtil.quoteStr(billId)  + ",";

        }

        billIds = billIds.substring(0,billIds.length()-1);

        int billEffectRows = 0;
        // 下面根据billTypeId与clearChargeTypeId进行综合判断
        if(billTypeId.longValue() == SysConstant.XT_DIC_BILL_TYPE.NATIVE_BILL.getId()){
            String statusFieldName = "";
            // 这是分单更新
            if(clearChargeTypeId.longValue() == SysConstant.XT_DIC_BILL_PAY_CLEARED_TYPE.NATIVE_BILL_AGENT_DELIVERY_CHARGE.getId()){
                // 这是分单代理送货费用结算更新
                statusFieldName = "agent_delivery_charge_status_id";
            }
            else if (clearChargeTypeId.longValue() == SysConstant.XT_DIC_BILL_PAY_CLEARED_TYPE.NATIVE_BILL_SERVICE_CHARGE.getId()){
                // 这是分单业务费结算更新
                statusFieldName = "business_cost_status_id";
            }
            // 执行更新
            if(!statusFieldName.isEmpty()){
                billEffectRows = bNativeBillService.updateStatusId(statusFieldName, statusId, billIds,this.getSysTime(),this.getLoginCzyId());
            }
        }
        else if(billTypeId.longValue() == SysConstant.XT_DIC_BILL_TYPE.AIR_MASTER_BILL.getId()){
            String statusFieldName = "";
            // 这是航空主单更新
            if(clearChargeTypeId.longValue() == SysConstant.XT_DIC_BILL_PAY_CLEARED_TYPE.AIR_MASTER_BILL_GROUND_CHARGE.getId()){
                // 这是航空主单地面运费结算状态更新
                statusFieldName = "ground_amount_status_id";
            }
            else if (clearChargeTypeId.longValue() == SysConstant.XT_DIC_BILL_PAY_CLEARED_TYPE.AIR_MASTER_BILL_TOTAL_AMOUNT.getId()){
                // 这是主单总金额结算状态更新,这是结算运输成本，并不是总运费
                statusFieldName = "transport_cost_status_id";
            }
            else if(clearChargeTypeId.longValue() == SysConstant.XT_DIC_BILL_PAY_CLEARED_TYPE.AIR_MASTER_BILL_AIRLINES_ADD_COST.getId()){
                // 这是主单航空附加费用结清
                statusFieldName = "airlines_add_cost_status_id";
            }
            else{
                // 这是主单其他费用
                statusFieldName = "other_charges_status_id";
            }
            if(!statusFieldName.isEmpty()){
                billEffectRows = bMasterBillFlightService.updateStatusId(statusFieldName,statusId,billIds);
            }
        }
        else if(billTypeId.longValue() == SysConstant.XT_DIC_BILL_TYPE.AUTO_MASTER_BILL.getId()){
            // 这是汽运主单更新
            if(clearChargeTypeId.longValue() == SysConstant.XT_DIC_BILL_PAY_CLEARED_TYPE.AUTO_MASTER_BILL_TOTAL_AMOUNT.getId()){
                // 这是汽运主单总运费更新
                String statusFieldName = "freight_amount_status_id";
                billEffectRows = bMasterBillAutoService.updateStatusId(statusFieldName,statusId,billIds);
            }
        }


        // 构建写入cw_bill_pay_cleared表对象
        int cwBillPayClearedEffectRows = 0;
        if(cwBillPayClearedPojos!= null && cwBillPayClearedPojos.size()>0){
            cwBillPayClearedEffectRows = cwPayMapper.batchInsert(cwBillPayClearedPojos);
        }

        return cwBillPayClearedEffectRows;
    }



}
